﻿// test_listnode.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
using namespace std;

template<typename T>
class ListNode
{
public:
    ListNode()
    {
        head = new Node(T());
        tail = head; //注意单链表的区别，tail非空，head为空
        size_ = 0;
    }

    void addFirst(T val)
    {
        Node* new_node = new Node(val);
        new_node->next = head->next;
        head->next = new_node;
        //易错
        if (size_ == 0)
        {
            tail = new_node;
        }
        size_++;
    }

    void addLast(T val)
    {
        Node* new_node = new Node(val);
        tail->next = new_node;
        tail = new_node;
        size_++;
    }

    void add(int index, T val)
    {
        checkPositionIndex(index);

        if (index == size_)
        {
            addLast(val);
            return;
        }

        Node* new_node = new Node(val);
        Node* current = head;
        for (int i = 0; i < index; i++)
        {
            current = current->next;
        }
        new_node->next = current->next;
        current->next = new_node;

        size_++;
    }


    T removeFirst()
    {
        if (isEmpty())
        {
            throw out_of_range("remove first error. list empty");
        }

        Node* first = head->next;
        head->next = first->next;

        if (size_ == 1)
        {
            tail = head;
        }

        T val = first->val;
        delete first;
        size_--;

        return val;
    }

    T removeLast()
    {
        if (isEmpty())
        {
            throw out_of_range("remove first error. list empty");
        }

        Node* prev = head;
        while (prev->next != tail)
        {
            prev = prev->next;
        }

        prev->next = nullptr;
        T val = tail->val;
        delete tail;
        tail = prev;

        size_--;

        return val;
    }

    T remove(int index)
    {
        if (isEmpty())
        {
            throw out_of_range("remove index error. list empty");
        }

        checkElementIndex(index);
        if (index == 0)
        {
            T val = removeFirst();
            return val;
        }
        if (index == size_ - 1)
        {
            T val = removeLast();
            return val;
        }

        Node* prev = head;
        for (int i = 0; i < index; i++)
        {
            prev = prev->next;
        }
        Node* del_node = prev->next;
        T val = del_node->val;
        prev->next = prev->next->next;
        delete del_node;
        size_--;

        return val;
    }

    T getFirst()
    {
        if (isEmpty())
        {
            throw out_of_range("get first error. list empty");
        }
        return head->next->val;
    }

    T getLast()
    { 
        if (isEmpty())
        {
            throw out_of_range("get last error.list empty");
        }
        return tail->val;
    }

    T get(int index)
    {
        if (isEmpty())
        {
            throw out_of_range("get index error.list empty");
        }
        Node* current = getNode(index);
        return current->val;
    }

    bool isEmpty()
    {
        return size_ == 0;
    }

private:
    struct Node
    {
        T val;
        Node* next;
        Node(T t) :val(t), next(nullptr) {};
    };
    Node* head;
    Node* tail;
    int size_;

    Node* getNode(int index)
    {
        checkElementIndex(index);
        Node* current = head->next;
        for (int i = 0; i < index; i++)
        {
            current = current->next;
        }
        return current;
    }

    bool isElementIndex(int index)
    {
        return index >= 0 || index < size_;
    }

    bool isPositionIndex(int index)
    {
        return index >= 0 || index <= size_;
    }

    void checkElementIndex(int index)
    {
        if (!isElementIndex(index))
        {
            throw out_of_range("element out of range");
        }
    }

    void checkPositionIndex(int index)
    {
        if (!isPositionIndex(index))
        {
            throw out_of_range("position out of range");
        }
    }

};



int main()
{
    vector<int>arr{ 1,2,3,4,5 };
    ListNode<int> list;
    list.addFirst(1);
    list.addFirst(2);
    list.addLast(3);
    list.addLast(4);
    list.add(2, 5);

    std::cout << list.removeFirst() << std::endl; // 2
    std::cout << list.removeLast() << std::endl; // 4
    std::cout << list.remove(1) << std::endl; // 5

    std::cout << list.getFirst() << std::endl; // 1
    std::cout << list.getLast() << std::endl; // 3
    std::cout << list.get(1) << std::endl; // 3
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
